package com.tgpms.vforms.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tgpms.common.Result;
import com.tgpms.exception.ExceptionExplain;
import com.tgpms.exception.QueryException;
import com.tgpms.interfaces.dto.VformInterface;
import com.tgpms.util.Common;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value = "AuthorityController", tags = "DM006 用户权限清单")
@RestController
@RequestMapping(value = "/ev/vform/authority")
public class AuthorityController {

	@Resource
    private VformInterface vformInterface;
	
	@ApiOperation(value = "查询权限信息", notes = "查询权限信息", httpMethod = "POST")
	@ApiImplicitParams({
    	@ApiImplicitParam(name = "pageNo",value = "页数",required = true,defaultValue = "1"),
    	@ApiImplicitParam(name = "pageSize",value = "每页数量",required = true,defaultValue = "10"),
    	@ApiImplicitParam(name = "dataowner",value = "数据拥有者",required = false),
    	@ApiImplicitParam(name = "usercode",value = "用户编码",required = true)
    })
    @PostMapping(value = "/selAuthorityList")
	public Result<List<Map<String, Object>>> selAuthorityList(
			@RequestParam(value = "pageNo", required = true,defaultValue = "1") String pageNo,
    		@RequestParam(value = "pageSize", required = true,defaultValue = "10") String pageSize,
    		@RequestParam(value = "dataowner", required = false) String dataowner,
    		@RequestParam(value = "usercode", required = true) String usercode
			) {
		Result<List<Map<String, Object>>> result = new Result<List<Map<String, Object>>>();
        List<Map<String, Object>> list = new ArrayList<>();
        if(Common.isEmpty(pageNo) || Common.isEmpty(pageSize) || Common.isEmpty(usercode)) {
            result.setSuccess(false);
            result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain());
            return result;
        }
        try {
        	JSONObject json = new JSONObject();
        	json.put("[SQL1]", "");
        	json.put("[SQL2]", "");
        	json.put("[SQL3]", " and a.user_name='"+usercode+"'");
        	//改前编码SCWZC047
            Result userResult = vformInterface.multiForMsQueryBySql("SXTCX006", json.toJSONString(), pageNo, pageSize,"Y","Y",dataowner);
            JSONObject userObject = JSONObject.parseObject(userResult.getData().toString());
            JSONArray userArray = JSONArray.parseArray(userObject.get("rows").toString());
            for (int i = 0; i < userArray.size(); i++) {
                Map<String, Object> tempMap = new HashMap<>();
                JSONObject jsonObject1 = userArray.getJSONObject(i);
                JSONObject menu = new JSONObject();
                menu.put("[SQL1]", "");
                menu.put("[SQL2]", "");
                menu.put("[SQL3]", " and s.security_code = '" + jsonObject1.get("SECURITY_CODE") + "'");
                Result menuResult;
                if (StringUtils.isEmpty(dataowner))
                    menuResult = vformInterface.multiForMsQueryBySql("SXTCX005", menu.toString(), "1", "1000","N","N", "");
                else
                    menuResult = vformInterface.multiForMsQueryBySql("SXTCX005", menu.toString(), "1", "1000","Y","Y",dataowner);
                JSONObject jsonObject = JSONObject.parseObject(menuResult.getData().toString());
                JSONArray jsonArray = JSONArray.parseArray(jsonObject.get("rows").toString());
                if (jsonObject1.get("DESCRIPTION") != null) {
                    tempMap.put("DESCRIPTION", jsonObject1.get("DESCRIPTION").toString());
                } else {
                    tempMap.put("DESCRIPTION", "");
                }
                if (jsonObject1.get("SECURITY_CODE") != null)
                    tempMap.put("SECURITY_CODE", jsonObject1.get("SECURITY_CODE").toString());
                tempMap.put("AUTHORITY", jsonArray);
                list.add(tempMap);
            }
            result.setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMsg(ExceptionExplain.ERROR_BY_QUERY.getExplain());
            throw new QueryException(ExceptionExplain.ERROR_BY_QUERY.getExplain());
        }
		return result;
        
	}
}
